package dji.ux.ext.setting.battery;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import dji.thirdparty.io.reactivex.android.schedulers.AndroidSchedulers;
import dji.ux.beta.R;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.SchedulerProvider;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;

public class SmartReturnPanel extends ConstraintLayoutWidget {
    private SmartReturnModel smartReturnModel;
    private SchedulerProvider schedulerProvider;
    private ToggleButton toggleButton;
    private boolean shouldReactToCheckChange;

    public SmartReturnPanel(Context context){
        super(context);
    }

    public SmartReturnPanel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SmartReturnPanel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        View view = inflate(context, R.layout.ext_panel_smart_return, this);
        schedulerProvider = SchedulerProvider.getInstance();
        toggleButton = view.findViewById(R.id.switch_smart_return);
        toggleButton.setOnCheckedChangeListener((buttonView, isChecked) -> handleSwitchChange(isChecked));
        setClickable(true);
        setFocusable(true);

        if (!isInEditMode()) {
            smartReturnModel = new SmartReturnModel(DJISDKModel.getInstance(),
                    ObservableInMemoryKeyedStore.getInstance(),
                    schedulerProvider);
        }

        setBackgroundColor(Color.BLACK);
        setPadding(25, 25, 25, 25);
    }

    private void handleSwitchChange(boolean isChecked) {
        if (shouldReactToCheckChange) {
            addDisposable(smartReturnModel.setEnable(isChecked).subscribe(() -> {
                // Do nothing
            }, logErrorConsumer(TAG, "switch smartReturnModel: ")));
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!isInEditMode()) {
            smartReturnModel.setup();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (!isInEditMode()) {
            smartReturnModel.cleanup();
        }
        super.onDetachedFromWindow();
    }

    @Override
    protected void reactToModelChanges() {
        addReaction(smartReturnModel.getSmartReturnEnabled()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::updateToggleButton));
    }

    private void updateToggleButton(boolean isEnable){
        shouldReactToCheckChange = false;
        toggleButton.setChecked(isEnable);
        shouldReactToCheckChange = true;
    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }
}
